package com.vogue.supplymall.order.domain.biz.impl;

import com.vogue.supplymall.common.domain.dto.SearchRefundInfo;
import com.vogue.supplymall.order.domain.biz.RefundOrderBiz;
import com.vogue.supplymall.order.domain.service.RefundOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Refund order biz.
 */
@Service("refundOrderBiz")
public class RefundOrderBizImpl implements RefundOrderBiz {
    /**
     * The constant log.
     */
    public static final Logger LOGGER_REFUNDORDERBIZIMPL = LoggerFactory.getLogger(OrderListBizImpl.class);

    @Resource
    private RefundOrderService refundOrderService;

    @Override
    public List<Map<String, Object>> findRefundOrderList(SearchRefundInfo searchRefundInfo) {
        List<Map<String, Object>> dataList = new ArrayList<>();
        List<Long> foreachList = new ArrayList<>();

        List<Map<String, Object>> result = refundOrderService.findRefundOrderList(searchRefundInfo);
        LOGGER_REFUNDORDERBIZIMPL.info("findRefundOrderList >> result.size : " + result.size());

        result.parallelStream().forEachOrdered((map) -> {
            if (!foreachList.contains(map.get("orderid"))) {
                foreachList.add((Long) map.get("orderid"));
            }

        });

        foreachList.parallelStream().forEachOrdered((orderid) -> {
            List<Map<String, Object>> goodsData = new ArrayList<>();
            Map<String, Object> order = new HashMap<>();
            result.stream().forEach((good) -> {
                if (orderid.equals(good.get("orderid"))) {

                    goodsData.add(good);
                    order.put("orderid", good.get("orderid"));
                    order.put("statusId", good.get("statusId"));
                    order.put("orderprice", good.get("orderprice"));
                    order.put("totalquantity", good.get("totalquantity"));
                    order.put("groupId", good.get("groupId"));
                    order.put("shopId", good.get("shopId"));
                    order.put("groupName", good.get("groupName"));
                    order.put("shopName", good.get("shopName"));
                    order.put("billState", good.get("billState"));
                    order.put("paymentmode", good.get("paymentmode"));
                    order.put("sellpullstate", good.get("sellpullstate"));
                    order.put("freight", good.get("freight"));
                    order.put("ordernumber", good.get("ordernumber"));
                    order.put("ordercreatedate", good.get("ordercreatedate"));
                    order.put("buytel", good.get("buytel"));
                    order.put("buyorganname", good.get("buyorganname"));
                    order.put("buyertype", good.get("buyertype"));

                }
            });

            order.put("goodsData", goodsData);
            dataList.add(order);
        });

        return dataList;
    }

}
